package com.roy.chatmemory;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;

/**
 * @author 山顶洞人郑某
 * @className FriendExample.java 记忆式聊天 可以记录上下文聊天内容
 * @date 2025 04 01
 */
public class FriendExample {

    public static void main(String[] args) {
        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .maxMessages(10)  //最大的历史记录数量
                .build();
        chatMemory.add(UserMessage.from("你好"));
        Response<AiMessage> response = model.generate(chatMemory.messages());
        System.out.println(response.content());
        //将上一轮的回答内容添加到上下文
        chatMemory.add(response.content());
        //再次问一个问题
        chatMemory.add(UserMessage.from("你重复一下"));
        System.out.println(model.generate(chatMemory.messages()).content());
    }
}
